using System.Collections;
using UnityEngine;
using TMPro;
using UnityEngine.SocialPlatforms.Impl; // For TextMeshPro

public class Cannon : MonoBehaviour
{
    private bool lockKey = false;
    public AudioSource hurtSFX;
    public AudioSource bubbleShotSFX;
    public GameController gameController;    
    public TargetPanel targetPanel;
    public TextMeshPro powerTextTMP; // Reference to the power text attached to the cannon
    private int currentPower = 0;
    public GameObject[] bubblePrefabs; // Array of bubble prefabs
    public Transform firePoint; // Point where bubbles will be instantiated

    private bool isPowerIncreasing = false; // Flag to check if power is increasing
    private Coroutine powerCoroutine; // Reference to the running coroutine

    public void InitiateCannon()
    {
        // Start increasing power text only if it's not already increasing
        if (!isPowerIncreasing && gameController.lives != 0)
        {
            powerCoroutine = null;
            powerCoroutine = StartCoroutine(IncreasePower()); // Store the running coroutine
        }
    }

    private IEnumerator IncreasePower()
    {
        isPowerIncreasing = true; // Start increasing power
        while (true)
        {
            int randomPower;

            // Weighted probability: 30% chance for targetText, 30% for random
            if (Random.Range(0f, 1f) < 0.3f)
            {
                randomPower = targetPanel.targetText; // Favor targetText value
            }
            else
            {
                randomPower = Random.Range(0, 100); // Generate a random number between 0 and 10
            }

            powerTextTMP.text = randomPower.ToString(); // Update power text
            currentPower = randomPower; // Update current power value
            yield return new WaitForSeconds(0.4f); // Wait 0.4 seconds before showing the next number

        }
    }


    public void ShootBubble()
    {
        if (!lockKey) // Prevents shooting bubble repeatedly
        {
            lockKey = true;
            // Stop power increase when shooting
            if (isPowerIncreasing)
            {
                StopCoroutine(powerCoroutine); // Stop the power increasing coroutine
            }

            // If the current power matches the target value, shoot a bubble
            if (currentPower == targetPanel.targetText)
            {
                // Choose a random bubble prefab from the array
                GameObject bubblePrefab = bubblePrefabs[Random.Range(0, bubblePrefabs.Length)];

                // Instantiate the bubble at the fire point
                bubbleShotSFX.Play();
                Instantiate(bubblePrefab, firePoint.position, Quaternion.identity);
                gameController.score += 10;
                gameController.UpdateScore();
                ClearCannon();
            }
            else
            {
                hurtSFX.Play();
                gameController.lives -= 1;
                gameController.hearts[gameController.lives].SetActive(false);
                ClearCannon();
            }
            lockKey = false;
            isPowerIncreasing = false; // Start increasing power
        }
    }

    public void ClearCannon()
    {
        powerTextTMP.text = ".."; // Clear power text
    }
}
